const {
	Controller
} = require('uni-cloud-router');
module.exports = class WorkController extends Controller {
	async add() {
	   const { ctx, service } = this;
	   const auth = await this.service.common.user.checkToken();
	   const uid = auth.uid;
	   return this.service.business.work.add(ctx.data,uid)
	}
	async del() {
	   const { ctx, service } = this;
	   const auth = await this.service.common.user.checkToken();
	   const uid = auth.uid;
	   const post = Object.assign(ctx.data, { uid });
	   return this.service.business.work.del(post)
	}
	/**
	 * 获取加班/调休列表
	 * @param {number} type 0:调休 ，1:加班"
	 */
	async getWorkList() {
	   const { ctx, service } = this;
	   const auth = await this.service.common.user.checkToken();
	   const uid = auth.uid;
	   let post ={}
	   console.log(ctx.data.uid)
	   if(ctx.data.uid){
		   //若有传uid则查询指定人的列表,否则默认查询登陆人
		    post = ctx.data;
	   }else{
		    post = Object.assign(ctx.data, { uid });
	   }
	   return this.service.business.work.getWorkList(post)
	}
	/**
	 * 获取加班/调休申请列表
	 * @param {String} uid 员工id
	 * @param {number} type 0:调休 ，1:加班"
	 */
	async getApplyList() {
		const { ctx, service } = this;
		const auth = await this.service.common.user.checkToken();
		const uid = auth.uid;
		const post = Object.assign(ctx.data, { uid });
		return this.service.business.work.getApplyList(post)
	}
	async getWorkReleaxLength() {
		//type  0:调休 ，1:加班",
	   const { ctx, service } = this;
	   const auth = await this.service.common.user.checkToken();
	   const uid = auth.uid;
	   const post = Object.assign(ctx.data, { uid });
	   return this.service.business.work.getWorkReleaxLength(post)
	}
	/**
	 * 获取需要审核加班/调休申请数量
	 * @param {String} uid 审核人员id
	 * @param {number} type 0:调休 ，1:加班"
	 */
	async getWorkApplyCount() {
		const { ctx, service } = this;
		const auth = await this.service.common.user.checkToken();
		const uid = auth.uid;
		const post = Object.assign(ctx.data, { uid });
		return this.service.business.work.getWorkApplyCount(post);
	}
	/**
	 * 获取需要审核加班/调休申请列表
	 * @param {String} uid 审核人员id
	 * @param {String} type 0:调休 ，1:加班"
	 */
	async getWorkApplyList() {
		const { ctx, service } = this;
		const auth = await this.service.common.user.checkToken();
		const uid = auth.uid;
		const post = Object.assign(ctx.data, { uid });
		return this.service.business.work.getWorkApplyList(post);
	}
	/**
	 * 获取申请详情
	 * @param {String} workId 申请id
	 */
	async getApplyDetail() {
		const { ctx, service } = this;
		const auth = await this.service.common.user.checkToken();
		const uid = auth.uid;
		const post = Object.assign(ctx.data, { uid });
		return this.service.business.work.getApplyDetail(post);
	}
	/**
	 * 更新申请项目负责人
	 * @param {String} workId 申请id
	 * @param {String} chargeId 项目负责人id
	 */
	async updateApplyCharge() {
		const { ctx, service } = this;
		const auth = await this.service.common.user.checkToken();
		const uid = auth.uid;
		const post = Object.assign(ctx.data, { uid });
		return this.service.business.work.updateApplyCharge(post);
	}
	/**
	 * 更新申请审核状态
	 * @param {String} workId 申请id
	 * @param {String} status 审核状态 0:待审核 ，1:已审核 ，2:已失效
	 */
	async updateApplyStatus() {
		const { ctx, service } = this;
		const auth = await this.service.common.user.checkToken();
		const uid = auth.uid;
		const post = Object.assign(ctx.data, { uid });
		return this.service.business.work.updateApplyStatus(post);
	}
	/**
	 * 更新审核记录审核状态
	 * @param {String} _id 审核记录id
	 * @param {String} status 审核状态 0:已通过 ，1:已拒绝 ,2:待审核
	 * @param {String} reason 拒绝原因
	 */
	async updateApplyAuditStatus() {
		const { ctx, service } = this;
		const auth = await this.service.common.user.checkToken();
		const uid = auth.uid;
		const post = Object.assign(ctx.data, { uid });
		
		// 当状态为拒绝时，直接将申请状态改为已失效
		if(ctx.data.status === '1') {
			await this.service.business.work.updateApplyStatus({workId: ctx.data.workId, status: '2'});
			return this.service.business.work.updateApplyAuditStatus(post);
		}
		
		// 获取超级管理员列表
		const adminList = await this.service.common.user.getUserByRole({role_id: 'admin'});
		let ifAdmin = false;
		if(adminList.code === 0) {
			for(let i in adminList.data) {
				if(adminList.data[i]._id === uid) {
					ifAdmin = true;
					break;
				}
			}
		}
		// 如果是超级管理员，直接将申请状态改为已审核
		if(ifAdmin) {
			this.service.business.work.updateApplyStatus({workId: ctx.data.workId, status: '1'});
		}
		// 如果不是超级管理员，则将申请转到超级管理员名下
		else {
			this.service.business.work.updateApplyCharge({workId: ctx.data.workId, chargeId: adminList.data[0]._id});
		}
		// 更新并返回审核记录状态
		return this.service.business.work.updateApplyAuditStatus(post);
	}
}
